package com.factory.iamp.dao;

import com.factory.iamp.pojo.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TableMapper {
    String base_table = "(select * from `table` where lid = #{lid}) as t1";

    String base_assembly_table = "(select * from table_assembly where lid = #{lid}) as t1";

    String base_main_table = "(select * from `table` where lid = #{lid} and ftid = -1) as t1";

    String reset_name_select_prev = "(select `tid`, `ftid`, `pre_cast_id`, `sell_id`, `org_id`, (" +
            "case when LENGTH(`name`) < 1 then CONCAT(IFNULL(`pre_cast_id`,''), '_', IFNULL(RIGHT(`sell_id`,4), '')) " +
            "else CONCAT(`name`, '_', IFNULL(`order_id`,''), '_', IFNULL(RIGHT(`sell_id`,4),'')) end) as `name`, " +
            "`creator`, `handler`, `type`, `header_id`, `header_value`, `body_list`, `body_value`, " +
            "`unfinished_content`, `tail_id`, `tail_value`, `problem`, `file_path`, `created_at`, `updated_at`, " +
            "`return_reason`, `return_process` from ";

    String reset_name_select = reset_name_select_prev + base_table + " where type > -1) as a ";

    String reset_name_main_select = reset_name_select_prev + base_main_table + " where type > -1) as a ";

    @Select("select count(tid) from " + base_table + " where type > 0")
    int Count(@Param("lid") int lid);

    @Select("select count(distinct tid) from question_log where sign = 'need'")
    int findUnfinishedProblemTableCount();

    @Select("select oid from question_log where tid = #{tid} and sign = 'need' order by oid limit 1")
    int countUnfinishedQuestion(@Param("tid") int tid);

    @Select("select count(tid) from " + reset_name_select + "where type > 0 and ${QueryStr}")
    int QueryCount(@Param("QueryStr") String QueryStr, @Param("lid") int lid);

    @Select({
            "select count(distinct tid) from question_log where list like '[%\"${pid}\"%]' and ",
            "${QueryStr} order by tid ${Order}"
    })
    int CountByProblem(@Param("pid") int pid, @Param("QueryStr") String QueryStr, @Param("Order") String Order);

    @Select({
            "select count(distinct question_log.tid) from question_log ",
            "join `table` where question_log.tid = `table`.tid ",
            "where `table`.ftid = -1 and list like '[%\"${pid}\"%]' and ",
            "${QueryStr} order by tid ${Order}"
    })
    int CountMainByProblem(@Param("pid") int pid, @Param("QueryStr") String QueryStr, @Param("Order") String Order);

    @Select("select count(tid) from " + reset_name_select + "where type = 3 and ${QueryStr}")
    int StoreCount(@Param("QueryStr") String QueryStr, @Param("lid") int lid);

    @Select("select count(tid) from " + reset_name_main_select + "where type = 3 and ${QueryStr}")
    int StoreMainCount(@Param("QueryStr") String QueryStr, @Param("lid") int lid);

    @Select("select count(tid) from " + base_table + " where type = 1 or type = 3")
    int PublishCount(@Param("lid") int lid);

    @Select(
            "select count(aid) from " + base_assembly_table + " where ${QueryStr}"
    )
    int findAssemblyCount(@Param("QueryStr") String QueryStr, @Param("lid") int lid);

    @Select("select count(tid) from " + reset_name_select + "where name like #{name}")
    int findByNameCount(@Param("name") String name, @Param("lid") int lid);

    @Select({
            "select count(tid) from " + reset_name_select + "where type > 0 and ",
            "(creator = #{uid} or handler = #{uid} or tid in (select tid from pass_log where `to` = #{uid})) and ",
            "${QueryStr}"
    })
    int findMyCount(@Param("uid") String uid, @Param("QueryStr") String QueryStr, @Param("lid") int lid);

    @Select({
            "select count(tid) from " + reset_name_main_select + "where type > 0 and ",
            "(creator = #{uid} or handler = #{uid} or tid in (select tid from pass_log where `to` = #{uid})) and ",
            "${QueryStr}"
    })
    int findMyCountMain(@Param("uid") String uid, @Param("QueryStr") String QueryStr, @Param("lid") int lid);

    @Select("select count(tid) from " + reset_name_select + "where creator = #{uid} and ${QueryStr}")
    int findByCreatorCount(@Param("uid") String uid, @Param("QueryStr") String QueryStr, @Param("lid") int lid);

    @Select("select count(tid) from " + reset_name_main_select + "where creator = #{uid} and ${QueryStr}")
    int findMainByCreatorCount(@Param("uid") String uid, @Param("QueryStr") String QueryStr, @Param("lid") int lid);

    @Select("select count(tid) from " + reset_name_select + "where handler = #{uid} and type > 0 and ${QueryStr}")
    int findByHandlerCount(@Param("uid") String uid, @Param("QueryStr") String QueryStr, @Param("lid") int lid);

    @Select("select count(tid) from " + reset_name_main_select + "where handler = #{uid} and type > 0 and ${QueryStr}")
    int findByHandlerMainCount(@Param("uid") String uid, @Param("QueryStr") String QueryStr, @Param("lid") int lid);

    @Select("select count(tid) from " + reset_name_main_select + " where sell_id in ${sids}")
    int findBySellidCount(@Param("sids") String sids, @Param("lid") int lid);

    @Select({
            "select count(tid) from " + reset_name_select + "where ",
            "tid in (select tid from pass_log where `to` = #{uid}) and ",
            "name like #{name} and created_at between #{begin} and #{end} and type in (",
            "<script>",
            "<foreach collection = 'typeList' item = 'item' index = 'index' separator = ','>",
            "${item}",
            "</foreach>)",
            "</script>"
    })
    int findMyPassedTableCount(
            @Param("uid") String uid, @Param("name") String name, @Param("typeList") List<Integer> typeList,
            @Param("begin") String begin, @Param("end") String end, @Param("lid") int lid
    );

    @Select({
            "select count(tid) from " + reset_name_main_select + "where ",
            "tid in (select tid from pass_log where `to` = #{uid}) and ",
            "name like #{name} and created_at between #{begin} and #{end} and type in (",
            "<script>",
            "<foreach collection = 'typeList' item = 'item' index = 'index' separator = ','>",
            "${item}",
            "</foreach>)",
            "</script>"
    })
    int findMainMyPassedTableCount(
            @Param("uid") String uid, @Param("name") String name, @Param("typeList") List<Integer> typeList,
            @Param("begin") String begin, @Param("end") String end, @Param("lid") int lid
    );

    @Select("select count(tid) from " + reset_name_select + "where type > 0 and ftid = #{tid}")
    int SubTableCount(@Param("tid") int tid, @Param("lid") int lid);

    @Select("select count(tid) from " + reset_name_select + "where type = 0 and creator = #{uid} and ${QueryStr}")
    int DraftCount(@Param("uid") String uid, @Param("QueryStr") String QueryStr, @Param("lid") int lid);

    @Select("select * from " + reset_name_select + "where type > 0 limit #{offset}, #{limit}")
    @Results(id = "table_user_info", value = {
            @Result(property = "creator", column = "creator"),
            @Result(property = "handler", column = "handler"),
            @Result(property = "tid", column = "tid"),
            @Result(
                    property = "creator_info", column = "creator",
                    one = @One(select = "com.factory.iamp.dao.UserMapper.findById")
            ),
            @Result(
                    property = "handler_info", column = "handler",
                    one = @One(select = "com.factory.iamp.dao.UserMapper.findById")
            ),
            @Result(
                    property = "unfinished_question", column = "tid",
                    one = @One(select = "com.factory.iamp.dao.TableMapper.countUnfinishedQuestion")
            ),
            @Result(
                    property = "subTable", column = "tid",
                    one = @One(select = "com.factory.iamp.dao.TableMapper.getAllSubTable")
            )
    })
    List<Table> ListPaged(@Param("offset") int offset, @Param("limit") int limit, @Param("lid") int lid);

    @Select("select * from " + reset_name_select + "where type > 0 and ${QueryStr} limit #{offset}, #{limit}")
    @ResultMap("table_user_info")
    List<Table> QueryListPaged(
            @Param("offset") int offset, @Param("limit") int limit, @Param("QueryStr") String QueryStr,
            @Param("lid") int lid
    );

    @Select({
            "select * from " + reset_name_select + "where type > 0 and tid in ",
            "(select distinct tid from question_log where list like '[%\"${pid}\"%]' and ${QueryStr}) ",
            "order by tid ${Order} limit #{offset}, #{limit}"
    })
    @ResultMap("table_user_info")
    List<Table> ListByProblemPaged(
            @Param("offset") int offset, @Param("limit") int limit,
            @Param("pid") int pid, @Param("QueryStr") String QueryStr, @Param("Order") String Order,
            @Param("lid") int lid
    );

    @Select({
            "select * from " + reset_name_main_select + "where type > 0 and tid in ",
            "(select distinct tid from question_log where list like '[%\"${pid}\"%]' and ${QueryStr}) ",
            "order by tid ${Order} limit #{offset}, #{limit}"
    })
    @ResultMap("table_user_info")
    List<Table> ListMainByProblemPaged(
            @Param("offset") int offset, @Param("limit") int limit,
            @Param("pid") int pid, @Param("QueryStr") String QueryStr, @Param("Order") String Order,
            @Param("lid") int lid
    );

    @Select({
            "select * from " + reset_name_select + "where type = 3 and ${QueryStr} ",
            "order by ${O} limit #{offset}, #{limit}"
    })
    @ResultMap("table_user_info")
    List<Table> StoreListPaged(
            @Param("offset") int offset, @Param("limit") int limit, @Param("QueryStr") String QueryStr,
            @Param("O") String Order, @Param("lid") int lid
    );

    @Select({
            "select * from " + reset_name_main_select + "where type = 3 and ${QueryStr} ",
            "order by ${O} limit #{offset}, #{limit}"
    })
    @ResultMap("table_user_info")
    List<Table> StoreMainListPaged(
            @Param("offset") int offset, @Param("limit") int limit, @Param("QueryStr") String QueryStr,
            @Param("O") String Order, @Param("lid") int lid
    );

    @Select(
            "select * from " + base_assembly_table + " where ${QueryStr} limit #{offset}, #{limit}"
    )
    List<TableAssembly> findAssemblyListPaged(
            @Param("offset") int offset, @Param("limit") int limit,
            @Param("QueryStr") String QueryStr, @Param("lid") int lid
    );

    @Select({
            "select * ",
            "from " + reset_name_select + " as t inner join archive_log as a ",
            "on t.tid = a.tid and a.archive_time between #{BeginDay} and #{EndDay}"
    })
    @ResultMap("table_user_info")
    List<Table> getArchiveDataByDay(
            @Param("BeginDay") String BeginDay, @Param("EndDay") String EndDay, @Param("lid") int lid
    );

    @Select("select * from " + base_table + " where name like #{name} and type > -1 limit #{offset}, #{limit}")
    @ResultMap("table_user_info")
    List<Table> findByNameListPaged(
            @Param("offset") int offset, @Param("limit") int limit, @Param("name") String name,
            @Param("lid") int lid
    );

    @Select({
            "select * from " + reset_name_select + "where type > 0 and ",
            "(creator = #{uid} or handler = #{uid} or tid in (select tid from pass_log where `to` = #{uid})) and ",
            "${QueryStr} limit #{offset}, #{limit}"
    })
    @ResultMap("table_user_info")
    List<Table> findMyListPaged(
            @Param("offset") int offset, @Param("limit") int limit,
            @Param("uid") String uid, @Param("QueryStr") String QueryStr, @Param("lid") int lid
    );

    @Select({
            "select * from " + reset_name_main_select + "where type > 0 and ",
            "(creator = #{uid} or handler = #{uid} or tid in (select tid from pass_log where `to` = #{uid})) and ",
            "${QueryStr} limit #{offset}, #{limit}"
    })
    @ResultMap("table_user_info")
    List<Table> findMyMainListPaged(
            @Param("offset") int offset, @Param("limit") int limit,
            @Param("uid") String uid, @Param("QueryStr") String QueryStr, @Param("lid") int lid
    );

    @Select("select * from " + reset_name_select + "where creator = #{uid} and ${QueryStr} limit #{offset}, #{limit}")
    @ResultMap("table_user_info")
    List<Table> findByCreatorListPaged(
            @Param("offset") int offset, @Param("limit") int limit,
            @Param("uid") String uid,@Param("QueryStr") String QueryStr, @Param("lid") int lid
    );

    @Select("select * from " + reset_name_main_select + "where creator = #{uid} and ${QueryStr} limit #{offset}, #{limit}")
    @ResultMap("table_user_info")
    List<Table> findMainByCreatorListPaged(
            @Param("offset") int offset, @Param("limit") int limit,
            @Param("uid") String uid,@Param("QueryStr") String QueryStr, @Param("lid") int lid
    );

    @Select("select * from " + reset_name_main_select + "where creator = #{uid} and ${QueryStr} limit #{offset}, #{limit}")
    @ResultMap("table_user_info")
    List<Table> findByCreatorMainListPaged(
            @Param("offset") int offset, @Param("limit") int limit,
            @Param("uid") String uid,@Param("QueryStr") String QueryStr, @Param("lid") int lid
    );

    @Select({
            "select * from " + reset_name_select + "where handler = #{uid} and type > 0 and ${QueryStr} ",
            "limit #{offset}, #{limit}"
    })
    @ResultMap("table_user_info")
    List<Table> findByHandlerListPaged(
            @Param("offset") int offset, @Param("limit") int limit,
            @Param("uid") String uid, @Param("QueryStr") String QueryStr, @Param("lid") int lid
    );

    @Select({
            "select * from " + reset_name_main_select + "where handler = #{uid} and type > 0 and ${QueryStr} ",
            "limit #{offset}, #{limit}"
    })
    @ResultMap("table_user_info")
    List<Table> findByHandlerMainListPaged(
            @Param("offset") int offset, @Param("limit") int limit,
            @Param("uid") String uid, @Param("QueryStr") String QueryStr, @Param("lid") int lid
    );

    @Select("select * from " + reset_name_select + "where sell_id in ${sids}")
    @ResultMap("table_user_info")
    List<Table> findBySellidListPaged(
            @Param("offset") int offset, @Param("limit") int limit,
            @Param("sids") String sids, @Param("lid") int lid
    );

    @Select({
            "select * from " + reset_name_select + "where tid in (select tid from pass_log where `to` = #{uid}) and ",
            "name like #{name} and created_at between #{begin} and #{end} and type in (",
            "<script>",
            "<foreach collection = 'typeList' item = 'item' index = 'index' separator = ','>",
            "${item}",
            "</foreach>)",
            "limit #{offset}, #{limit}",
            "</script>"
    })
    @ResultMap("table_user_info")
    List<Table> findMyPassedTableListPaged(
            @Param("offset") int offset, @Param("limit") int limit, @Param("uid") String uid,
            @Param("name") String name, @Param("typeList") List<Integer> typeList,
            @Param("begin") String begin, @Param("end") String end, @Param("lid") int lid
    );

    @Select({
            "select * from " + reset_name_main_select + "where tid in (select tid from pass_log where `to` = #{uid}) and ",
            "name like #{name} and created_at between #{begin} and #{end} and type in (",
            "<script>",
            "<foreach collection = 'typeList' item = 'item' index = 'index' separator = ','>",
            "${item}",
            "</foreach>)",
            "limit #{offset}, #{limit}",
            "</script>"
    })
    @ResultMap("table_user_info")
    List<Table> findMainMyPassedTableListPaged(
            @Param("offset") int offset, @Param("limit") int limit, @Param("uid") String uid,
            @Param("name") String name, @Param("typeList") List<Integer> typeList,
            @Param("begin") String begin, @Param("end") String end, @Param("lid") int lid
    );

    @Select({
            "select * from " + reset_name_select + "where ",
            "type > 0 and (ftid = #{tid} or tid = #{tid}) limit #{offset}, #{limit}"
    })
    @ResultMap("table_user_info")
    List<Table> SubTableListPaged(
            @Param("offset") int offset, @Param("limit") int limit, @Param("tid") int tid, @Param("lid") int lid
    );

    @Select({
            "select * from " + reset_name_select + "where type = 0 and creator = #{uid} and ${QueryStr} ",
            "limit #{offset}, #{limit}"
    })
    @ResultMap("table_user_info")
    List<Table> DraftListPaged(
            @Param("offset") int offset, @Param("limit") int limit,
            @Param("uid") String uid, @Param("QueryStr") String QueryStr, @Param("lid") int lid
    );

    @Select("select * from " + reset_name_select + "where creator = #{user} and tid = #{tid} limit 1")
    @ResultMap("table_user_info")
    Table findByIdCreator(@Param("tid") int tid, @Param("user") String user, @Param("lid") int lid);

    @Select("select * from " + reset_name_select + "where tid = #{tid} limit 1")
    @ResultMap("table_user_info")
    Table findById(@Param("tid") int tid, @Param("lid") int lid);

    @Select("select count(*) from pass_log where tid = #{tid} and `to` <> #{uid} limit 1")
    int passToOtherCount(@Param("tid") int tid, @Param("uid") String uid);

    @Select("select * from " + reset_name_select + "where type = 2 and tid = #{tid} limit 1")
    @ResultMap("table_user_info")
    Table findArchivedById(@Param("tid") int tid, @Param("lid") int lid);

    @Select("select * from " + reset_name_select + "where type = 1 and tid = #{tid} limit 1")
    @ResultMap("table_user_info")
    Table findPublishById(@Param("tid") int tid, @Param("lid") int lid);

    @Select("select * from " + reset_name_select + "where tid = #{tid} limit 1")
    @Results({
            @Result(property = "creator", column = "creator"),
            @Result(property = "handler", column = "handler"),
            @Result(property = "tid", column = "tid"),
            @Result(
                    property = "creator_info", column = "creator",
                    one = @One(select = "com.factory.iamp.dao.UserMapper.findById")
            ),
            @Result(
                    property = "handler_info", column = "handler",
                    one = @One(select = "com.factory.iamp.dao.UserMapper.findById")
            ),
            @Result(
                    property = "subTable", column = "tid",
                    one = @One(select = "com.factory.iamp.dao.TableMapper.getAllSubTable")
            )
    })
    TableDetail findPublishByIdDetail(@Param("tid") int tid, @Param("lid") int lid);

    @Select("select tid from `table` where ftid = #{tid}")
    List<Integer> getAllSubTable(@Param("tid") int tid);

    @Select("select * from " + reset_name_select + "where type = 3 and tid = #{tid} limit 1")
    @ResultMap("table_user_info")
    Table findStoreById(@Param("tid") int tid, @Param("lid") int lid);

    @Select({
            "select * from " + reset_name_select + "where ",
            "handler = #{handler} and type <> 2 and type <> 0 and tid = #{tid} limit 1"
    })
    @ResultMap("table_user_info")
    Table findHandlePublishById(@Param("tid") int tid, @Param("handler") String handler, @Param("lid") int lid);

    @Select({
            "select * from " + reset_name_select + "where ",
            "(creator = #{creator} or handler = #{creator}) and type <> 2 and tid = #{tid} limit 1"
    })
    @ResultMap("table_user_info")
    Table findSelfPublishById(@Param("tid") int tid, @Param("creator") String creator, @Param("lid") int lid);

    @Select("select * from " + reset_name_select + "where creator = #{creator} and type = 0 and tid = #{tid} limit 1")
    @ResultMap("table_user_info")
    Table findSelfDraftById(@Param("tid") int tid, @Param("creator") String creator, @Param("lid") int lid);

    @Select("select * from " + reset_name_select + "where creator = #{creator} and type = 0 and name = #{name} limit 1")
    @ResultMap("table_user_info")
    Table findSelfDraftByName(@Param("name") String name, @Param("creator") String creator, @Param("lid") int lid);

    @Select("select * from " + base_assembly_table + " where aid = #{aid}")
    TableAssembly findAssemblyById(@Param("aid") int aid, @Param("lid") int lid);

    @Select("select * from " + base_assembly_table + " where name = #{name}")
    TableAssembly findAssemblyByName(@Param("name") String name, @Param("lid") int lid);

    @Select({
            "select * from (select check_log.*, t.name, u.username from check_log ",
            "inner join (select * from " + reset_name_select + "where a.name like #{name}) as t ",
            "on t.tid = check_log.tid ",
            "inner join `user` as u on u.uid = check_log.uid) as s where ${QueryStr} order by uid"
    })
    List<CheckLogQuery> checkLogQuery(
            @Param("name") String name, @Param("QueryStr") String QueryStr, @Param("lid") int lid
    );

    @Select("select * from check_log where tid = #{tid}")
    List<CheckLog> getTableCheckTime(@Param("tid") int tid);

    @Select("select * from question_log where tid = #{tid}")
    List<QuestionLog> getTableProblemAll(@Param("tid") int tid);

    @Select("select * from question_log where tid = #{tid} and sign != 'need'")
    List<QuestionLog> getTableProblemSign(@Param("tid") int tid);

    @Select({
            "select * from " + reset_name_select + "where tid in ",
            "(select tid from question_log where sign = 'need') limit #{offset}, #{limit}"
    })
    @ResultMap("table_user_info")
    List<Table> findUnfinishedProblemTableListPaged(
            @Param("offset") int offset, @Param("limit") int limit, @Param("lid") int lid
    );

    @Select("select * from check_log where type = #{type} and ${QueryStr}")
    CheckLogQuery findCheckLog(@Param("type") int type, @Param("QueryStr") String QueryStr);

    @Insert({
            "<script>",
            "insert into `table`",
            "(lid, name, creator, handler, type, header_id, header_value, body_list, body_value, ",
            "tail_id, tail_value, created_at, updated_at) ",
            "values (#{lid}, #{name}, #{creator}, #{handler}, 0, #{header_id}, #{header_value}, '[",
            "<foreach collection = 'body_list' item = 'item' index = 'index' separator = ','>",
            "${item}",
            "</foreach>",
            "]', #{body_value}, #{tail_id}, #{tail_value}, NOW(), NOW())",
            "</script>"
    })
    @Options(useGeneratedKeys = true, keyProperty = "tid", keyColumn = "tid")
    void addDraftTable(Table table);

    @Insert("insert into archive_log(tid) values(#{tid})")
    void addArchiveLog(@Param("tid") int tid);

    @Insert("insert into check_log(uid, tid, oid, type, created_at) values(#{uid}, #{tid}, #{oid}, #{type}, NOW())")
    void addCheckLog(@Param("uid") String uid, @Param("tid") int tid, @Param("oid") int oid, @Param("type") int type);

    @Insert("insert into pass_log(tid, `from`, `to`) values(#{tid}, #{from}, #{to})")
    void addPassLog(@Param("tid") int tid, @Param("from") String from, @Param("to") String to);

    @Insert({
            "<script>",
            "insert into table_assembly",
            "(lid, name, creator, header_id, body_list, tail_id, title, description) ",
            "values (#{lid}, #{name}, #{creator}, #{header_id}, '[",
            "<foreach collection = 'body_list' item = 'item' index = 'index' separator = ','>",
            "${item}",
            "</foreach>",
            "]', #{tail_id}, #{title}, #{description})",
            "</script>"
    })
    void addAssemblyTable(TableAssembly data);

    @Update({
            "<script>",
            "update table_assembly set name = #{name}, creator = #{creator}, header_id = #{header_id}, ",
            "body_list = '[",
            "<foreach collection = 'body_list' item = 'item' index = 'index' separator = ','>",
            "${item}",
            "</foreach>",
            "]', tail_id = #{tail_id}, title = #{title}, description = #{description} where aid = #{aid}",
            "</script>"
    })
    void alterAssemblyTable(TableAssembly data);

    @Update("update `table` set ${name} = #{dataid} where tid = #{tid}")
    void updateId(@Param("tid") int tid, @Param("name") String name, @Param("dataid") String dataid);

    @Update({
            "<script>",
            "update `table` set name = #{name}, handler = #{handler}, header_id = #{header_id}, ",
            "header_value = #{header_value}, tail_id = #{tail_id}, tail_value = #{tail_value}, ",
            "body_list = '[",
            "<foreach collection = 'body_list' item = 'item' index = 'index' separator = ','>",
            "${item}",
            "</foreach>",
            "]', body_value = #{body_value}, updated_at = NOW() where tid = #{tid}",
            "</script>"
    })
    void alterDraftTable(Table table);

    @Update("update `table` set unfinished_content = #{oid} where tid = #{tid}")
    void setUnfinishedContent(@Param("tid") int tid, @Param("oid") int oid);

    @Update("update `table` set type = 1, updated_at = NOW() where tid = #{tid} and type = 0 and creator = #{uid}")
    void publishDraftTable(@Param("tid") int tid, @Param("uid") String uid);

    @Update("update `table` set type = 3, updated_at = NOW() where tid = #{tid} and type = 0 and creator = #{uid}")
    void storeDraftTable(@Param("tid") int tid, @Param("uid") String uid);

    @Update({
            "update `table` set type = 2, problem = #{problems}, updated_at = NOW() ",
            "where tid = #{tid} and type <> 2 and type <> 0"
    })
    void archiveTable(@Param("tid") int tid, @Param("problems") String problems);

    @Update("update `table` set type = 3, updated_at = NOW() where tid = #{tid} and type <> 2 and type <> 0")
    void storeTable(@Param("tid") int tid);

    @Update({
            "update `table` set type = 1, handler = #{uid}, updated_at = NOW() ",
            "where tid = #{tid} and type <> 2 and type <> 0"
    })
    void claimTable(@Param("tid") int tid, @Param("uid") String uid);

    @Update("update `table` set file_path = #{path} where tid = #{tid}")
    void addFilePath(@Param("tid") int tid, @Param("path") String path);

    @Update("update `table` set handler = #{uid} where tid = #{tid}")
    void passTableToUid(@Param("tid") int tid, @Param("uid") String uid);

    @Update("update `table` set return_reason = #{reason}, return_process = #{process} where tid = #{tid}")
    void returnFactory(@Param("tid") int tid, @Param("reason") String reason, @Param("process") String process);

    @Update("update table_content set ${column} = 'need' where tid = ${tid} and oid = ${oid}")
    void deleteCheckFile(@Param("tid") int tid, @Param("oid") int oid, @Param("column") String column);

    @Update("update `table` set type = -1 where tid = ${tid}")
    void deleteTable(@Param("tid") int tid);

    @Update("update `table` set ftid = ${ftid} where tid = ${tid}")
    void setFatherTable(@Param("tid") int tid, @Param("ftid") int ftid);

    @Delete("delete from `table` where tid = #{tid} and type = 0 and creator = #{uid}")
    void deleteDraftTable(@Param("tid") int tid, @Param("uid") String uid);

    @Delete("delete from table_assembly where ${QueryStr}")
    void deleteAssemblyTable(@Param("QueryStr") String QueryStr);

    @Delete("delete from check_log where type = #{type} and ${QueryStr}")
    void deleteCheckLog(@Param("type") int type, @Param("QueryStr") String QueryStr);
}
